package helpers;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.EOFException;
import java.io.IOException;
import java.net.Socket;
import java.util.Vector;

import objects.DominoGame;

/**
 * In this class the connection is answered and the message is parsed
 * **/
public class GamerThread extends Thread {
	String request;
	DataInputStream in;
	DataOutputStream out;
	Socket clientSocket;
	private DominoGame dominoGame;
	private int playerNumber;
	private boolean active;

	public GamerThread(Socket aClientSocket, DominoGame dominoGame) {
		try {
			this.dominoGame = dominoGame;
			clientSocket = aClientSocket;
			in = new DataInputStream(clientSocket.getInputStream());
			out = new DataOutputStream(clientSocket.getOutputStream());

		} catch (IOException e) {
			System.out.println("Connection:" + e.getMessage());
		}
	}

	public void parseMsg() {
		String[] arrayAux = request.split("_");// addplayer_Amaya
		String command = "pass";
		if (arrayAux[0] != null) {
			command = arrayAux[0];
		}

		if (command.equals("addplayer")) {

			String player_name = arrayAux[1];
			int player_number = this.dominoGame.addPlayer(player_name, this);
			answer("player_" +player_name+"_"+ player_number);

			this.playerNumber = player_number;
			setActive(false);

		} else if (command.equals("last")) {
			String player_name = arrayAux[1];
			int player_number = this.dominoGame.addPlayer(player_name, this);
			answer(player_name + "_" + player_number);
			
			this.playerNumber = player_number;
			setActive(false);
			this.dominoGame.broadcastStartGame();

		} else if (command.equals("move")) {
			// "moveline#_[a,b]"
			String line_number = arrayAux[1];
			String tuple = arrayAux[2];
			this.dominoGame.move(this.playerNumber,
					Integer.valueOf(line_number), tuple);

		} else if (command.equals("ask")) {

			this.dominoGame.stealDomino(this.playerNumber);

		}else if (command.equals("pass")) {
			String line_number = arrayAux[1];
			this.dominoGame.passLine(this.playerNumber);

		} else if (command.equals("finishline")) {
			// "moveline#_[a,b]"
			String line_number = arrayAux[1];
			String tuple = arrayAux[2];
			this.dominoGame.move(this.playerNumber,
					Integer.valueOf(line_number), tuple);
		} else if (command.equals("other")) {

		} else {

			System.out.println("Command not understood");
		}

	}

	synchronized public  void answer(String reply) {

		try {
			System.out.println(reply);
			out.writeBytes(reply+"\n");
			out.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("Failed to answer back to player");
			e.printStackTrace();
		}

	}

	public void run() {
		try {
			String data = "";
			while (true) {// This thread will manage this player/client
				if(active){
					data = in.readUTF(); // read a line of data from the stream
					System.out.println("Answering:" + data);

					this.request = data;
					parseMsg();
					// parse matrix size and operation
				}else{
					sleep(100);
				}
				

			}

		} catch (EOFException e) {
			System.out.println("EOF:" + e.getMessage());
		} catch (IOException e) {
			System.out.println("readline:" + e.getMessage());
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				clientSocket.close();
			} catch (IOException e) {/* close failed */
			}
		}

	}

	public void setActive(boolean b) {
		this.active=b;
		
	}

}